﻿using leaf.Entity;
using System;

namespace leaf.Base
{
    /// <summary>
    /// 全局变量类
    /// </summary>
    public static class Global
    {
        //日志打印工具配置
        public static log4net.ILog log = log4net.LogManager.GetLogger("wx_log");
        //
        private static string appid = BaseSupport.GetConfig("appid");
        private static string state = BaseSupport.GetConfig("state");
        private static string secret = BaseSupport.GetConfig("secret");

        //打印日志分割线
        public static void Log_line()
        {
            log.Info("-----------------------------------------------------------------------------------------------------\r\n");
        }

        //项目文件夹
        public static string BaseDir = AppDomain.CurrentDomain.BaseDirectory;

        //微信调用凭据
        private static WXBaseInfo WXBaseInfo;

        /// <summary>
        /// 获取微信基础信息
        /// </summary>
        /// <returns></returns>
        public static WXBaseInfo GetWXBaseInfo()
        {
            return WXBaseInfo;
        }

        /// <summary>
        /// 赋值微信基础信息
        /// </summary>
        /// <param name="wxBaseInfo"></param>
        public static void SetWXBaseInfo(WXBaseInfo wxBaseInfo)
        {
            Global.WXBaseInfo = wxBaseInfo;
        }

        /// <summary>
        /// 设置微信菜单的uri
        /// </summary>
        /// <returns></returns>
        public static string SetWXMenuUri()
        {
            string uri = " https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
            uri = uri.Replace("ACCESS_TOKEN", Global.GetWXBaseInfo().Access_token);
            return uri;
        }

        /// <summary>
        /// 获取微信通信密钥的uri
        /// </summary>
        /// <returns></returns>
        public static string GetWXAccessTokenUri()
        {
            string uri = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
            uri = uri.Replace("APPID", appid).Replace("APPSECRET", secret);
            return uri;
        }

        /// <summary>
        /// 获取用户信息的uri
        /// </summary>
        /// <param name="getUserAccessToken"></param>
        /// <param name="openId"></param>
        /// <returns></returns>
        //public static string GetWXUserInfoUri(string getUserAccessToken, string openId)
        //{
        //    string uri = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
        //    uri = uri.Replace("ACCESS_TOKEN", getUserAccessToken).Replace("OPENID", openId);
        //    return uri;
        //}

        /// <summary>
        /// 重定向到微信指定的网页的Uri
        /// </summary>
        /// <param name="">目标uri</param>
        /// <returns></returns>
        public static string RedirectUri(string targetUri)
        {
            string uri = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
            uri = uri.Replace("APPID", appid).Replace("REDIRECT_URI", targetUri).Replace("SCOPE", "snsapi_base").Replace("STATE", state);
            return uri;
        }

        /// <summary>
        /// 获取用户信息的AccessToken的Uri
        /// </summary>
        /// <param name="code">换取access_token的票据</param>
        /// <returns></returns>
        public static string GetUserAccessTokenUri(string code)
        {
            string uri = " https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
            uri = uri.Replace("APPID", appid).Replace("SECRET", secret).Replace("CODE", code);
            return uri;
        }
    }
}